Skip to content

fix: correct base pointer for polymorphic class with non-polymorphic base - #6296

Open
henryiii wants to merge 7 commits into
pybind:masterfrom
henryiii:fix/issue-1080
Open

henryiii wants to merge 7 commits into
pybind:masterfrom
henryiii:fix/issue-1080

Conversation

@henryiii

@henryiii henryiii commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

— codex on behalf of Ralf —

Description

A polymorphic C++ class can place a non-polymorphic base subobject at a nonzero offset. The single-inheritance fast path used the derived pointer as a base pointer, so inherited member access could read the wrong memory.

For py::class_<Derived, Base>, the binding now records that the derived class may need pointer adjustment and uses the registered C++ upcast when converting to the base. A zero-offset sibling registered with py::class_<Sibling>(m, "Sibling", base_handle) keeps its direct load path. The regression tests cover both classes, including in Python multiple inheritance.

The offset repair requires the template-base form, which supplies the C++ types needed for the upcast. The new flag lives in the temporary type_record and feeds the existing type_info::simple_ancestors field, so no PYBIND11_INTERNALS_VERSION bump is needed.

Historical background

The original report in September 2017 used a Python base handle. A fix proposed two days later required an internals ABI bump, so its author deferred it past v2.2.1 and suggested v2.3. That proposal remains open. Original report, 2017 proposal

Suggested changelog entry:

  • Fix base-class access when a polymorphic C++ class is bound with a non-polymorphic base via py::class_<Derived, Base>.

…base

A polymorphic derived class puts its vptr before a non-polymorphic base,
so the base subobject is not at offset 0. The single-inheritance fast
path in load_impl used the derived pointer as the base pointer, and
base members read the wrong memory. Use the multiple-inheritance path
for this case, which applies the offset.

Assisted-by: ClaudeCode:claude-fable-5-2
@henryiii
henryiii marked this pull request as ready for review November 23, 2026 22:33
@rwgk

rwgk commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

— codex on behalf of Ralf —

No actionable findings. When a polymorphic derived type has a non-polymorphic base, the registered C++ cast handles the potentially nonzero subobject offset. My probe read the wrong base member on the parent and the expected value at this head, including for a Python subclass of the bound derived class. All 21 tests in the two multiple-inheritance modules passed on regular and free-threaded CPython 3.15.2. Base registration, Regression

@rwgk

rwgk commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

At second look, somehow (even though I wasn't asking for a re-review) codex figured out that this PR has a compatibility regression. I'm posting the entire response, even only the first paragraph is actionable. The rest is mainly to document how my prodding unexpectedly produced the finding.


I’d still merge the typed-base fix, but I’d hold the current #6296 head for one compatibility regression. I reproduced it with two classes sharing Base. A zero-offset Plain bound with py::class_<Plain>(m, "Plain", base) worked before. Once a separate polymorphic sibling is registered with the new typed-base fix, both Plain().d and passing Plain to a function taking const Base& raise TypeError. Marking the shared Base non-simple disables the shortcut that Plain relied on, and the handle-based binding has no registered C++ upcast. Binding both subclasses with template bases passes. The docs explicitly present the handle form as supported, so I’d ask Henry to preserve this case before merging. PR #6296, Documented binding forms, [Reproducer and logs: /wrk/codex_scratch_space/logs/pybind11-board-2026-11-28/pr6296-history]

The nine years are misleading. Someone proposed a fix just two days after #1080 was reported in September 2017. It changed the internals ABI, so the author deferred it from v2.2.1 to v2.3. It’s still open, and the public discussion ends after an MSVC linker fix. #6296 is smaller because a template base gives pybind11 the C++ types it needs to reuse the existing pointer-adjusting cast, just as your virtual-inheritance fix did. The original #1080 example passes the base as a Python handle, and I confirmed that example still reads the wrong value at this head. Original report, 2017 fix attempt, Virtual-inheritance fix

Your rarity hypothesis is plausible. The trigger requires mixed polymorphism, a nonzero base offset, and an operation that actually goes through the base binding. As Henry pointed out in the #6306 discussion, users can find workarounds and move on. The issue history gives us little basis for estimating frequency. Making Base polymorphic can sidestep this layout, and it can also change the class’s ABI. I verified a binding-only workaround on the parent commit using py::class_<Derived, Base>(..., py::multiple_inheritance()). Slack thread, Henry’s #6306 reply, Binding flag

rwgk added 3 commits November 28, 2026 12:11
Track the potential vptr offset on the polymorphic source class. Require simple source ancestry for the unadjusted load path so a sibling bound with a Python class handle can keep using that path.

@rwgk rwgk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@henryiii could you please re-review from your end?

PyPy raises "instance layout conflicts in multiple inheritance" when a
Python class inherits from two bound classes and one has a bound C++ parent.

Assisted-by: ClaudeCode:claude-fable-5-2
A class bound with a Python base handle has no registered C++ cast. When
the parent has non-simple ancestry, the load of the child as the parent
found no match and raised TypeError. Register an identity cast for this
case, because the handle form already requires equal pointers.

Add tests for template-bound and handle-bound children of the pybind#1080
class and for a shared_ptr load of the base. Document when the template
form of the base is necessary.

Assisted-by: ClaudeCode:claude-fable-5-2
@henryiii

Copy link
Copy Markdown
Collaborator Author

I needed to do another 12 line fix on there. There's a speed regression too, we could only avoid that with an ABI break, if this goes in we can add that to the ABI list discussion. Here are options for simplification on this PR. The first one, which revert part of my fix and most of yours, would take the change out of load_impl, which is the hot path.

🤖 AI text below 🤖

Yes. The largest saving is to drop the source-side machinery from 180094a6 now that the identity cast exists. I have not built or tested this.

Option 1: return to the original 5-line fix plus the identity cast.

  • Restore the 750e67c6 form: set rec.multiple_inheritance = true when the class is polymorphic and the base is not.
  • Remove the if (!parent_simple_ancestors) condition, so each handle-bound single base gets the identity cast.
  • 180094a6 was needed because the original fix made I1080Base non-simple, and the handle-bound sibling I1080Plain then had no cast. The identity cast gives it one, so Case 2c succeeds.
  • This removes the type_record flag, the attr-inl.h and internals.h edits, and all changes to load_impl. The header diff goes from about 45 changed lines in 6 files to about 17 lines in 2 files (pybind11.h, pybind11-inl.h).
  • Cost: handle-bound siblings such as I1080Plain load through try_implicit_casts instead of the fast path, and each handle-bound class adds one entry to its parent's implicit_casts.
  • This reverts Ralf's design, so he must agree.

Option 2: keep the current design and trim it.

  • Revert the no_cpp_mi → simple_target rename and the rewritten Case 2c comment in type_caster_base.h. This leaves two changed conditions there.
  • Revert the comment edit in internals.h, so the PR does not touch that file.
  • Replace the has_cast loop with std::any_of.
  • Remove test_handle_base_with_polymorphic_sibling; the parametrized PyMulti test already loads I1080Plain as the base, except on PyPy where that test is xfailed.

Each base given as a Python handle now gets an identity cast, so a
handle-bound sibling of a polymorphic class can load through the
registered casts. The polymorphic class sets multiple_inheritance again,
and the type_record flag and the load_impl changes are removed.

Assisted-by: ClaudeCode:claude-fable-5-2
@henryiii

Copy link
Copy Markdown
Collaborator Author

I have the simpler version, can push. It's like the original path, but with the identity cast I think it is better than before. Less that have the LoC, and avoids the higher risk hot path changes. All the new tests pass unchanged. Two downsides, below. I think I'll push it once CI runs on this version.

🤖 AI text below 🤖

  • Slow path for handle-bound siblings: I1080Plain now loads as I1080Base through try_implicit_casts, not through the fast path.
  • Wider behavior: a handle-bound child now loads as its parent also when a later multiple-inheritance descendant makes the parent non-simple. On master this raised TypeError.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants